package com.huadi.firematerialident.controller;

import com.huadi.firematerialident.po.FireMaterialEvidenceInfo;
import com.huadi.firematerialident.service.FireMaterialEvidenceInfoServiceImpl;
import com.huadi.firematerialident.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/me")
public class MEController {

    @Autowired
    FireMaterialEvidenceInfoServiceImpl fireMaterialEvidenceInfoService;
    @Autowired
    UserServiceImpl userService;

    @RequestMapping("/MEAdd")
    public String addME(
            @RequestParam("FireInfoID") String fid,
            @RequestParam("METype") String metype,
            @RequestParam("MoreInfo") String moreinfo, Model model){

        System.out.println("adiwuhdauw");

        FireMaterialEvidenceInfo fireMaterialEvidenceInfo = new FireMaterialEvidenceInfo();
        fireMaterialEvidenceInfo.setMEID();
        fireMaterialEvidenceInfo.setFireInfoID(fid);
        fireMaterialEvidenceInfo.setMEType(metype);
        fireMaterialEvidenceInfo.setMoreInfo(moreinfo);

        if(fireMaterialEvidenceInfoService.addFireMaterialEvidenceInfo(fireMaterialEvidenceInfo)==0){
            model.addAttribute("msg","添加失败");
        }
        else{
            model.addAttribute("msg","添加成功");
        }
        return "me/mecommit";
    }

    @RequestMapping("/getAllME")
    @ResponseBody
    public String getAllME(Model model){


        List<Map<String,Object>> list = fireMaterialEvidenceInfoService.getAllFireMaterialEvidenceInfo();
        if(list.isEmpty()){
            model.addAttribute("msg","没有找到数据");
            return "/me/melist";
        }
        else{
            return userService.ListToJSON(list.size(),list);
        }
    }
}
